package GUI;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.util.concurrent.ExecutionException;

import javax.swing.AbstractAction;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.JScrollPane;
import javax.swing.JTable;

import model.MyTableModel;

/**
 * Load sets of arguments from a file
 * User select 5 sets before lunching the application
 * @author LoLo
 *
 */
public class MainFrame extends JFrame {
	
	//>>RESOURCES
	
	//VARIABLES
	//size
	int w,h;
	//COMPONENTS
	//buttons
	private JButton quit;
	private JButton start;
	private JPanel controlPanel;
	//progress bar
	JLabel prog_title;
	public static JProgressBar progress;
	//sets of arguments list
	private JScrollPane scrollPane;
	private MyTableModel model;
	private JTable table; //where display data
	//EVENTS
	private Action quitAction;
	private Action startAction;
	//Single or multi thread
	private String thread;
	
	//>>FUNCTIONS
	
	//CONSTRUCTOR
	public MainFrame(String thread){
		//super class
		super();
		
		//frame settings
		//size
		w=980;
		h=400;
		setMinimumSize(new Dimension(w,h));
		setLocationRelativeTo(null); //centered
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setLayout(new BoxLayout(this.getContentPane(),BoxLayout.PAGE_AXIS)); //layout
		
		//components
		model=new MyTableModel();
		table = new JTable(model);
		//fix columns' width
		table.setAutoResizeMode(JTable.AUTO_RESIZE_SUBSEQUENT_COLUMNS);
		MyTableRenderer rend = new MyTableRenderer();
		for(int i=1;i<model.getColumnCount();i++) table.getColumnModel().getColumn(i).setCellRenderer(rend); 
		table.setForeground(Color.BLUE);
		scrollPane= new JScrollPane(table);
		scrollPane.setMinimumSize(new Dimension(w,h-100));
		this.add(scrollPane);
		//buttons
		controlPanel = new JPanel();
		quit = new JButton("Quit");
		quit.setForeground(Color.red);
		controlPanel.add(quit);
		start = new JButton("Start");
		start.setForeground(Color.green);
		controlPanel.add(start);
		//Label
		prog_title = new JLabel("Computation : ");
		prog_title.setVisible(false);
		//progressBar
		controlPanel.add(prog_title);
		progress = new JProgressBar();
		progress.setValue(0);
		progress.setStringPainted(true);
		progress.setVisible(false);
		//progress.
		controlPanel.add(progress);
		this.add(controlPanel);
		
		//events
		quitAction = new Action("quit");
		quit.setAction(quitAction);
		startAction = new Action("start");
		start.setAction(startAction);
		
		//Single or multi thread
		this.thread=thread;
	}
	
	
	
	/**
	 * Event : button clicked
	 * 
	 */
	private class Action extends AbstractAction {
		
		String toDo= null; //what to do
		
		/**
		 * Event constructor
		 * @param s type of action (expl quit, send daily mail etc)
		 */
		public Action(String s) {
			toDo=s;
		
			if(toDo.equals("quit")){
				putValue(NAME, "<html><font size=\"4\">Quit</font></html>");
				putValue(SHORT_DESCRIPTION, "Quit Application");
			}
			else if(toDo.equals("start")){
				putValue(NAME, "<html><font size=\"4\">Start</font></html>");
				putValue(SHORT_DESCRIPTION, "Start Monte Carlo computing");
			}
			
		}
		/**
		 * Action performed, 1 action / type of event
		 */
		public void actionPerformed(ActionEvent e) {
			//QUIT PROGRAM
			if(toDo.equals("quit")){ //button quit
				//ask for confirmation
				JOptionPane jop = new JOptionPane();
				jop.setVisible(true);
				int option = JOptionPane.showConfirmDialog(null, "Exit application ?", "", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
				//exit
				if(option==0){
					dispose();
				}
				
			}
			//START APPLI
			else if(toDo.equals("start")){ //button start
				
				//enough checked sets ?
				if(model.getSelectedSets().size() != 1 ){
					JOptionPane.showMessageDialog(null, "You must have chosen exactly 1 set before starting the program.", "Wrong number of selected item(s)",JOptionPane.INFORMATION_MESSAGE);
				}
				else{
					//New thread to be able to display progress
					Thread t = new Thread(){
						
						public void run(){
							try {
								//Display progress
								prog_title.setVisible(true);
								progress.setVisible(true);
								model.getMc().monteCarloCalculation(model.getSelectedSets().get(0),thread);
								
							} catch (InterruptedException | ExecutionException e1) {
								// TODO Auto-generated catch block
								//e.printStackTrace();
							}
							
							//display results in a pop up window
							JOptionPane.showMessageDialog(null, model.getMc().getResultObject().toString() ,"Monte Carlo calculation : RESULTS",JOptionPane.INFORMATION_MESSAGE);
							
							//Hide progress data
							prog_title.setVisible(false);
							progress.setVisible(false);
							
						}
					};t.start();									
				}
			}
			
		}
	}

	
	
	//ACCESSORS

	public JProgressBar getProgress() {
		return progress;
	}
}
